package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.mainService.mapper.RoleServiceMapper;
import com.uchiha.nova.mainService.mapper.ServiceMapper;
import com.uchiha.nova.mainService.service.IServiceService;
import com.uchiha.nova.mainService.wrapper.ServiceWrapper;
import com.uchiha.nova.core.model.entity.RoleServiceEntity;
import com.uchiha.nova.core.model.entity.ServiceEntity;
import com.uchiha.nova.core.model.vo.RoleSelectVO;
import com.uchiha.nova.core.model.vo.ServiceVO;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * controller表  服务实现类
 *
 * @author Chill
 */
@Service
public class ServiceServiceImpl extends BaseServiceImpl<ServiceMapper, ServiceEntity> implements IServiceService {

    @Resource
    RoleServiceMapper roleServiceMapper;

    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    public List<ServiceVO> getTree(Long parentId) {
        List<ServiceEntity> list = this.baseMapper.selectList(new QueryWrapper<ServiceEntity>().eq("parent_id",parentId));
        List<ServiceVO> vos = ServiceWrapper.build().listVO(list);
        return vos;
    }

    @Override
    public List<ServiceVO> getTreeALl() {
        List<ServiceEntity> list = this.baseMapper.selectList(null);
        List<ServiceVO> vos = ServiceWrapper.build().listNodeVO(list);
        return vos;
    }

    @Override
    @Transactional
    public Boolean saveRoleSelectVO(RoleSelectVO roleSelectVO) {
        roleServiceMapper.delete(new QueryWrapper<RoleServiceEntity>().eq("role_id",roleSelectVO.getRoleId()));
        Long roleId = roleSelectVO.getRoleId();
        roleSelectVO.getIds().forEach(item->{
            RoleServiceEntity roleServiceEntity = new RoleServiceEntity();
            roleServiceEntity.setRoleId(roleId);
            roleServiceEntity.setServiceId(item);
            roleServiceMapper.insert(roleServiceEntity);
        });
        return true;
    }

    @Override
    public List<String> getRoleHasSelect(Long roleId) {
        List<RoleServiceEntity> roleServiceEntities = roleServiceMapper.selectList(new QueryWrapper<RoleServiceEntity>().eq("role_id", roleId));
        List<String> ids = roleServiceEntities.stream().map(item->item.getServiceId().toString()).collect(Collectors.toList());

        return ids;
    }
}
